//
//  XYEmotionTool.swift
//  SinaWB_XY
//
//  Created by 小熊猫 on 15/10/23.
//  Copyright © 2015年 小熊猫. All rights reserved.
//

import UIKit

let EMOTION_ROW_MAX_NUM = 3
let EMOTION_COL_MAX_NUM = 7
let EMOTION_MAX_NUM = EMOTION_ROW_MAX_NUM  * EMOTION_COL_MAX_NUM - 1

class XYEmotionTool: NSObject {
    
    //最近表情的范围
    static var reRange:NSRange = {
        return  NSMakeRange(0,1)
        }()
    
    //默认表情的范围
    static let defaultRange:NSRange = {
        
         return  NSMakeRange(reRange.length,(defaultEmotion.count - 1) / EMOTION_MAX_NUM + 1)
    }()
    
    //Emoji表情的范围
    static let emojiRange:NSRange = {
        let location = defaultRange.length + reRange.length
        return NSMakeRange(location,(emojiEmotion.count - 1) / EMOTION_MAX_NUM + 1)
    }()
    
    //浪小花表情的范围
    static let lxhRange:NSRange = {
        let location = emojiRange.length + defaultRange.length + reRange.length
        return NSMakeRange(location,(lxhEmotion.count - 1) / EMOTION_MAX_NUM + 1)
        }()

    //最近表情集合
    static var reEmotion:[XYEmotionModel] =  {
        
        //从文档中取表情
        let path = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("RecentEmotion.archive")
        let serult = NSKeyedUnarchiver.unarchiveObjectWithFile(path)
        
        if serult == nil{
            return [XYEmotionModel]()
        }else{
            return serult as! [XYEmotionModel]
        }
        
    }()
       
    
    
    //默认表情的表情集合
    static let defaultEmotion:[XYEmotionModel] = {
        return XYEmotionTool.loadEmotion("EmotionIcons/default/info.plist")
    }()

    //Emoji表情的表情集合
    static let emojiEmotion:[XYEmotionModel] = {
        return XYEmotionTool.loadEmotion("EmotionIcons/emoji/info.plist")
        }()
    
    //浪小花表情的表情集合
    static let lxhEmotion:[XYEmotionModel] = {
        return XYEmotionTool.loadEmotion("EmotionIcons/lxh/info.plist")
        }()

    //总共表情的集合
    class func allEmotionNum() -> Int{
        let reEmotionNum = reRange.length
        let defaultEmotionNum = defaultRange.length
        let emojiEmotionNum = emojiRange.length
        let lxhEmotionNum = lxhRange.length
        
        return reEmotionNum + emojiEmotionNum + defaultEmotionNum + lxhEmotionNum
    }
    
    //加载plist文件
    class func loadEmotion(filePath:String) -> [XYEmotionModel]{
        
        let path = NSBundle.mainBundle().pathForResource(filePath, ofType: nil)
        
        let array = NSArray(contentsOfFile: path!)
        //创建一个装表情模型的数组
        var emojiArray = [XYEmotionModel]()
        
        //遍历数组.转模型
        for value in array!{
            let dict = XYEmotionModel(dict: (value as! [String:AnyObject]))
            
            dict.emojiPath = (path! as NSString).stringByDeletingLastPathComponent
            
            emojiArray.append(dict)
        }
        return emojiArray
    }
 
    
    //给一个页数.返回这个页数所在的表情类型
    class func emotionWithPageNum(pageNum:Int) -> XYKeybordToolBarType{
        if pageNum == 0{
            return .Recent
        }else if (NSLocationInRange(pageNum, defaultRange)){
            return .Default
        }else if (NSLocationInRange(pageNum, emojiRange)){
            return .Emoji
        }else{
            return .Lxh
        }
    }
    //给一个页数.计算这页在所在类型的第几页
    class func  emotionSeleNumWithPageNum(pageNum:Int) -> Int{
        if pageNum == 0{
            return reRange.length
        }else if (NSLocationInRange(pageNum, defaultRange)){
            return pageNum - defaultRange.location
        }else if (NSLocationInRange(pageNum, emojiRange)){
            return pageNum - emojiRange.location
        }else{
            return pageNum - lxhRange.location
        }

    }
    
    //给一个类型.返回这个页数所在的表情的长度
    class func emotionlengthWithButtonType(type:XYKeybordToolBarType) -> Int{
        switch type{
        case .Recent :
            return reRange.length
        case .Default :
            return defaultRange.length
        case .Emoji :
            return emojiRange.length
        case .Lxh :
            return lxhRange.length
        }
    }

    
    //给一个type.返回这个type的按钮所对应的表情开始页
    class func emotionWithButtonType(type:XYKeybordToolBarType) -> Int{
        switch type{
        case .Recent :
            return reRange.location
        case .Default :
            return defaultRange.location
        case .Emoji :
            return emojiRange.location
        case .Lxh :
            return lxhRange.location
        }
    }
    
    
    //给一个页数,返回所在页数对应的表情模型
    class func emotionCellPageModelWithPageNum(pageNum:Int) -> [XYEmotionModel]{
        
        //传进来一个页数.一个表情集合
        func typeModelWithPageNum(pageNum:Int,emotion:[XYEmotionModel]) -> [XYEmotionModel]{
            
            //通过页数,计算在这个表情机会的第几页
            let page = emotionSeleNumWithPageNum(pageNum)
            
            //计算location
            let loc = page * EMOTION_MAX_NUM
            
            //计算length
            
            var len = EMOTION_MAX_NUM
            
            if (loc + EMOTION_MAX_NUM) > emotion.count{
                len = emotion.count - loc
            }
            
            let range = NSMakeRange(loc, len)
                        
            let result = (emotion as NSArray).subarrayWithRange(range)
            
            return result as! [XYEmotionModel]
        }
        //计算出这一页是哪个表情
        let type = emotionWithPageNum(pageNum)
        
        //判断表情.截取表情模型
        switch type{
        case .Recent :
            return reEmotion
        case .Default :
            return typeModelWithPageNum(pageNum, emotion: defaultEmotion)
        case .Emoji :
            return typeModelWithPageNum(pageNum, emotion: emojiEmotion)
        case .Lxh :
            return typeModelWithPageNum(pageNum, emotion: lxhEmotion)
        }
        
    }
    
    
    //存点击按钮之后的表情
    class func saveClickButtonEmotion(emotion:XYEmotionModel){
    
        let path = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("RecentEmotion.archive")
        
        
        //判断集合中是否保存这个表情
        let isHas =  reEmotion.contains(emotion)
        //如果存在就删除掉
        if isHas{
            let index = (reEmotion as NSArray).indexOfObject(emotion)
            reEmotion.removeAtIndex(index)
        }
        
        
        //将表情放到最近表情集合里面
        reEmotion.insert(emotion, atIndex: 0)
        
        if reEmotion.count > 20{
            reEmotion.removeLast()
        }
        
        //将最近表情存到文件中去
         NSKeyedArchiver.archiveRootObject(reEmotion, toFile: path)
    }
    
    
}



